import axios from '@/netWork';
import { pagination, data, loading } from './data';
import { btnType } from './enum';
import { ElMessage, ElMessageBox } from 'element-plus';
let serachInfo: object = {};
let newDialogDatas: object = {};
function getData(): void {
    loading.value = true;
    let Query = {
        ...pagination,
        ...serachInfo
    };
    axios({
        url: '/students/check',
        method: 'GET',
        params: Query
    }).then((res: any) => {
        if (res.code === 200) {
            data.tableData = res.results;
            data.total = res.total;
        }
    });
}
function changeBtn(val: any): void {
    if (val.btnName === btnType.edit) {
        data.dialogDatas = {
            ...val.row
        };
        data.dialogConfig = {
            dialogVisible: true,
            title: '编辑',
            beforeClose: false,
            trueBtnText: '确人编辑',
            falseBtnText: '取消'
        };
    } else if (val.btnName === btnType.delete) {
        ElMessageBox.confirm('是否确定永久删除该数据？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
        })
            .then(() => {
                axios({
                    url: '/students/delete',
                    method: 'DELETE',
                    data: {
                        id: val.row.id
                    }
                }).then((res: any) => {
                    if (res.code === 200) {
                        ElMessage({
                            type: 'success',
                            message: '删除成功'
                        });
                    } else {
                        ElMessage({
                            type: 'error',
                            message: res.data
                        });
                    }
                    getData();
                });
            })
            .catch(() => {
                ElMessage({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        console.log(2222);
    }
}
function handleCurrentChange(val: number): void {
    pagination.pageNum = val;
    getData();
}
function searchData(val: any): void {
    pagination.pageNum = 1;
    serachInfo = { ...val };
    getData();
}
function addTrue(): void {
    data.dialogDatas = {
        name: '',
        phone: '',
        address: '',
        age: '',
        type: '',
        imgUrl: ''
    };
    data.dialogConfig = {
        dialogVisible: true,
        title: '新增',
        beforeClose: false,
        trueBtnText: '确定',
        falseBtnText: '取消',
        beforeData: {
            title: '是否确定要关闭弹窗',
            trueBtnText: '确定111',
            falseBtnText: '取消222',
            type: 'warning'
        }
    };
}
function submitDialog(val: any): void {
    switch (val.title) {
        case '新增':
            addSure(val.row);
            break;
        case '编辑':
            editSure(val.row);
            break;
        default:
            break;
    }
}
function addSure(val: any): void {
    axios({
        url: '/students/add',
        method: 'POST',
        data: val
    }).then((res: any) => {
        if (res.code === 200) {
            ElMessage({
                type: 'success',
                message: '添加成功'
            });
            getData();
            data.dialogConfig.dialogVisible = false;
        } else {
            ElMessage({
                type: 'error',
                message: res.msg
            });
        }
    });
}
function editSure(val: any): void {
    axios({
        url: '/students/edit',
        method: 'PUT',
        data: val
    }).then((res: any) => {
        if (res.code === 200) {
            ElMessage({
                type: 'success',
                message: '编辑成功'
            });
            getData();
            data.dialogConfig.dialogVisible = false;
        } else {
            ElMessage({
                type: 'error',
                message: res.msg
            });
        }
    });
}
export { getData, changeBtn, handleCurrentChange, searchData, addTrue, submitDialog };
